package gui;

import java.awt.Cursor;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.io.IOException;

import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;

import org.xml.sax.SAXException;

import util.Constants.EVEData;
import ctrl.DBAccess;
import ctrl.EVECentralCtrl;
import data.EVEItem;
import data.EVESystem;
import data.ItemSystemCollection;

public class ListContextMenu extends JPopupMenu implements ActionListener {

  private EVEData               _data;
  private ProductGroupSplitPane _collectionPane;
  private EVECentralCtrl        _evecctrl;
  private JMenuItem             _update;
  private JMenuItem             _remove;

  public ListContextMenu(EVECentralCtrl evecctrl, DBAccess dataBase) {
    _evecctrl = evecctrl;

    _update = new JMenuItem("update market data");
    _remove = new JMenuItem("remove");
    _update.addActionListener(this);
    _remove.addActionListener(this);
    add(_update);
    add(_remove);
    setVisible(false);
  }

  public void showMenu(EVEData data, MouseEvent event, ProductGroupSplitPane splitPane) {
    _data = data;
    _collectionPane = splitPane;
    setLocation((int) event.getLocationOnScreen().getX(), (int) event.getLocationOnScreen().getY());
    setVisible(true);
  }

  /**
   * TODO: remove clone why is there 2 times the database updatequery block?
   * removes always the first element of the list
   */
  public void _remove() {
    if (_data.equals(EVEData.Systems)) {
      int[] indizes = _collectionPane.getSystemList().getSelectedIndices();
      EVESystem[] systems = new EVESystem[indizes.length];
      for (int i = 0; i < indizes.length; i++) {
        systems[i] = _collectionPane.getSystemList().getModel().get(i);
      }

      for (int i = 0; i < indizes.length; i++) {
        EVESystem selectedSystem = systems[i];

        _collectionPane.getCollection().removeSystem(selectedSystem);

        if (!_collectionPane.getSystemList().getModel().removeElement(selectedSystem)) {
          System.out.println("could not remove System:" + selectedSystem.getSystemName());
        }
        for (int j = 0; j < _collectionPane.getItemList().getModel().size(); j++) {
          EVEItem item = _collectionPane.getItemList().getModel().get(j);
          item._removeSystem(selectedSystem);
        }
      }
    }
    else if (_data.equals(EVEData.Items)) {
      int[] indizes = _collectionPane.getItemList().getSelectedIndices();
      EVEItem[] items = new EVEItem[indizes.length];
      for (int i = 0; i < indizes.length; i++) {
        items[i] = _collectionPane.getItemList().getModel().get(i);
      }

      for (int i = 0; i < items.length; i++) {
        EVEItem selectedItem = items[i];

        _collectionPane.getCollection().removeItem(selectedItem);

        if (!_collectionPane.getItemList().getModel().removeElement(selectedItem)) {
          System.out.println("could not remove Item:" + selectedItem.getItemName());
        }
        for (int j = 0; j < _collectionPane.getSystemList().getModel().size(); j++) {
          EVESystem system = _collectionPane.getSystemList().getModel().get(j);
          system._removeItem(selectedItem);
        }
      }
    }
  }

  public void _updateMarketData() {
    try {
      setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR));
      if (_data.equals(EVEData.Systems)) {
        _evecctrl._retrieveMarketData(_collectionPane.getSystemList().getModel().get(_collectionPane.getSystemList().getSelectedIndex()));
      }
      else if (_data.equals(EVEData.Items)) {
        _evecctrl._retrieveMarketData(_collectionPane.getItemList().getModel().get(_collectionPane.getItemList().getSelectedIndex()));
      }
      setCursor(Cursor.getDefaultCursor());
    }
    catch (IOException | SAXException e) {
      e.printStackTrace();
    }
  }

  @Override
  public void actionPerformed(ActionEvent event) {
    if (event.getSource().equals(_update)) {
      _updateMarketData();
    }
    else if (event.getSource().equals(_remove)) {
      _remove();
    }
    setVisible(false);
  }

  @Override
  public void setVisible(boolean value) {
    if (_data != null && _data.equals(EVEData.Items)) {
      if (_collectionPane.getItemList().getSelectedItem() == null) {
        super.setVisible(false);
      }
      else {
        super.setVisible(value);
      }
    }
    else if (_data != null && _data.equals(EVEData.Systems)) {
      if (_collectionPane.getSystemList().getSelectedSystem() == null) {
        super.setVisible(false);
      }
      else {
        super.setVisible(value);
      }
    }
    else {
      super.setVisible(value);
    }
  }
}
